package com.wondream.myframework.app.basictest.baidurank;

import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class AnyBaiduKeywordRank {
//    @Test
//    public void test(){
//        Connection.Response res = Jsoup.connect("https://www.baidu.com/link?url=NsHkjUPGl_mZhzM-TUlu6ZfyO3os9ubGKhDv9XiMklKxONjEtFTVLyrKWiW50gJzRFL2hChm_hkarUGQkmwPS_").timeout(60000).method(Connection.Method.GET).followRedirects(false).execute();
//        String str= res.header("Location");
//    }

    public static String request(String url) {
        StringBuffer res = new StringBuffer();
        HttpURLConnection conn = null;
        try {
            URL serverUrl = new URL(url);
            conn = (HttpURLConnection) serverUrl.openConnection();
            conn.setRequestMethod("GET");// "POST" ,"GET"
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.connect();
            InputStream ins = conn.getInputStream();
            String charset = "UTF-8";
            InputStreamReader inr = new InputStreamReader(ins, charset);
            BufferedReader bfr = new BufferedReader(inr);
            String line = "";
            do {
                res.append(line);
                line = bfr.readLine();
            } while (line != null);
            inr.close();
            bfr.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return res.toString();
    }

    public static void main(String[] args) throws Exception {
        String keyword = "uiseo";
//        String site = "www.uiseo.net";
        String site = "qifeiseo";
        keyword = URLEncoder.encode(keyword, "utf-8");
        int p = 1;
        int s = 10;
        String url = "http://www.baidu.com/s?wd=%s&pn=%s&ie=utf-8&usm=1&rsv_page=1";
        boolean isFound = false;
        while (p<11) {
            int pn = (p - 1) * s;
            String _url = String.format(url, keyword, pn + "");
//            System.out.println("Request url " + _url);
//            String html = HttpUtil.get(url);
            String html = request(_url);
//            System.out.println(html);

            Document document = Jsoup.parse(html);
            Elements showUrl = document.select("a.c-showurl");
            for(int i = 0 ; i<showUrl.size() ; i++){
                    Element element = showUrl.get(i);
                String linkText = element.text();
//                String linkHref = element.attr("href");
//                System.out.println(linkHref);
//                System.out.println(linkText);
                if (linkText.indexOf(site) != -1) {
                    System.out.println("当前排名："+(i+1));
                    isFound = true;
                    break;
                }
            }
            if(isFound){
                break;
            }
            p++;
        }

    }
}
